//
//  main.m
//  demo1-Foundation的使用
//
//  Created by Maggie on 15/8/8.
//  Copyright (c) 2015年 fmning. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //字典，无序的集合，每一个元素都是成对出现【key:value】
        NSMutableArray *goods = [[NSMutableArray alloc]init];
        
        NSDictionary *dic1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"iphone6",@"name",@"4500",@"price", nil];
        NSLog(@"dic1 count:%lu",[dic1 count]);
        
        //简写语法只用于不可变类型
        NSArray *images = @[@"1.png",@"2.png"];
        NSDictionary *dic2 = @{
                               @"name":@"iphone6p",
                               @"price":@"5500",
                               @"images":images
                               };
        
        //遍历字典的方法
        NSArray *keys = [dic2 allKeys];
        for (NSString *key in keys) {
            NSString *value = [dic2 valueForKey:key];
            NSLog(@"%@:%@",key,value);
        }
        
        //把商品添加到数组中
        [goods addObject:dic1];
        [goods addObject:dic2];
        
        //把数组goods保存为一个plist文件
        [goods writeToFile:@"goods.plist" atomically:YES];
        
        //打印所有的商品信息
        for (NSDictionary *good in goods) {
            
            NSLog(@"%@,%@",[good valueForKey:@"name"],[good valueForKey:@"price"]);
        }
        for (int i = 0; i < [goods count]; i++) {
            NSDictionary *good = goods[i];
            NSString *name = [good valueForKey:@"name"];
            NSString *price = [good valueForKey:@"price"];
            
            NSLog(@"%@,%@",name,price);
            
        }
        
        //可变字典
        NSMutableDictionary *mutabDic = [[NSMutableDictionary alloc] init];
        [mutabDic setValue:@"iphone4" forKey:@"name"];
        [mutabDic setValue:@"2000" forKey:@"price"];
        
        
        //
        NSDictionary *dict = [[NSDictionary alloc]init];
        
        //导航栏
        NSArray *conditionArr = [[NSArray alloc]initWithObjects:@"销量",@"评价",@"星级", nil];
    }
    return 0;
}

void testString(){
    //NSString的使用
    NSString *name = @"tom";
    NSLog(@"name address:%p",name);
    name = @"maggie";
    NSLog(@"name address:%p",name);
    
    NSString *msg = [[NSString alloc]initWithFormat:@"hello,%@",name];
    
    
    //NSMutableString的使用
    NSMutableString *str = [[NSMutableString alloc]init];
    [str appendString:@"hello"];
    [str appendString:@" world!"];
    
    NSRange range;
    range.location = 0;
    range.length = 5;
    
    [str replaceCharactersInRange:range withString:@"hi"];
    
    [str deleteCharactersInRange:range];
    
    //重置，可以清空字符串
    [str setString:@""];
    
    NSLog(@"str= %@",str);
}

void testArray(){
    //OC数组NSArray
    NSArray *typeArray = [[NSArray alloc]initWithObjects:@"电子",@"包",@"女装", nil];
    
    typeArray = @[@"电子",@"包",@"女装"];
    
    for (int i = 0; i < [typeArray count]; i++) {
        NSString *type = [typeArray objectAtIndex:i];
        NSLog(@"%@",type);
    }
    
    for (NSString *type in typeArray) {
        NSLog(@"%@",type);
    }
    
    //NSArray是一个有序的集合，里面的每一个成员都有一个索引号
    //NSArray数组的元素必须是对象类型(id类型)!!!
    
    int a = 12;
    
    //装箱
    NSNumber *intNum = [NSNumber numberWithInt:a];
    NSNumber *boolNum = [NSNumber numberWithBool:YES];
    NSArray *arr = [[NSArray alloc]initWithObjects:@"test",intNum,boolNum, nil];
    
    //拆箱
    a = [intNum intValue];
    
    //NSMutableArray可变数组
    NSMutableArray *typeArr2 = [[NSMutableArray alloc]init];
    [typeArr2 addObject:@"衣服"];
    [typeArr2 addObject:@"手机"];
}



